// pages/cardjihuo/cardjihuo.js
const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendTime: '获取验证码',
    daojishi: 60,
    popShow: false, //弹窗
    kahao: '',
    kami: '',
    tel: '',
    yanzheng_code: '',
    code:'',
    fsing:false,
    tjing:false
  },

  Activation_card() {
    let that = this;

    if (!(/^1[3456789]\d{9}$/.test(that.data.tel))) {
      wx.showToast({
        title: '请正确填写手机号',
        icon: 'none',
        duration: 2000,
      });
      return false;
    }
    if (that.data.kahao == '') {
      wx.showToast({
        title: '请输入正确的卡号或卡密',
        icon: 'none'
      })
      return
    }
    if (that.data.yanzheng_code != that.data.code||that.data.yanzheng_code=='') {
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'none',

      })
      return
    }

    if(!that.data.tjing){
      that.setData({
        tjing:true
      })
    let data = {
      kahao: that.data.kahao,
      kami: that.data.kami,
      tel: that.data.tel,
    };
  
    common.request(api.Activation_card, data, 'post').then((res) => {
      if (res.status == 1) {
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
        that.setData({
          kahao: '',
          kami: '',
          tel: '',
          tjing:false,
          yanzheng_code:'',
          popShow:true
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        that.setData({
          tjing:false
        })
      }
    })
  }
  },
  //获取验证码
  sendCode: function () {
    // 60秒后重新获取验证码
    let that = this;
    let tel = that.data.tel;
    if (!(/^1[345678]\d{9}$/.test(tel))) {
      wx.showToast({
        title: '请正确填写手机号',
        icon: 'none',
        duration: 2000,
      });
      return false;
    }
    
    if(that.data.fsing){
      wx.showToast({
        title: '不可重复发送',
      })
      return
    }
that.setData({
  fsing:true
})
    common.request(api.get_Sendcode, {
      tel: tel
    }, 'post').then((res) => {
      if (res.status == 1) {
        var inter = setInterval(function () {
          that.setData({
            smsFlag: true,
            sendTime: that.data.daojishi + 's后重发',
            daojishi: that.data.daojishi - 1,
            code: res.code,
          });
       
          if (that.data.daojishi < 0) {
            clearInterval(inter);
            that.setData({
              smsFlag: false,
              sendTime: '获取验证码',
              daojishi: 60,
              code:'',
              fsing:false
            });
           
          }
          console.log(that.data.daojishi)
        }.bind(that), 1000);
      }
    })
  },
  canCel() {
    this.setData({
      popShow: false
    })
  },
  gotaocan(){
    wx.switchTab({
      url: '/pages/yuding/yuding',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      popShow:false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})